package com.item.controller;


import com.github.pagehelper.PageInfo;
import com.item.bean.RPage;
import com.item.domain.Chapter;
import com.item.domain.TUsers;
import com.item.po.GoodsPo;
import com.item.po.TUsersPo;
import com.item.service.TUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TUsersController {

    @Autowired
    TUsersService tUsersService;

    @PostMapping(value = "/tUsers/{limit}/{page}")
    public RPage<TUsersPo> TUsers(
            @PathVariable(value = "limit", required = false) int limit,
            @PathVariable(value = "page", required = false) int page,
            @RequestBody(required = false) TUsers tUsers) {
        List<TUsersPo> tUserss = tUsersService.findAll(limit, page, tUsers);
        PageInfo<TUsersPo> pageInfo = new PageInfo<>(tUserss);
        RPage<TUsersPo> rPage = new RPage<>();
        rPage.setList(pageInfo.getList());
        rPage.setPageNum(pageInfo.getPageNum());
        rPage.setTotal(pageInfo.getTotal());
        rPage.setPages(pageInfo.getPages());
        rPage.setPageSize(pageInfo.getPageSize());

        return rPage;
    }

    @PostMapping(value = "/tUsers/add")
    public Integer tUsersAdd(@RequestBody TUsersPo tUsersPo) {
        TUsers tUsers = new TUsers();

        tUsers.setUserName(tUsersPo.getUserName());
        tUsers.setPassWord(tUsersPo.getPassWord());
        tUsers.setTelePhone(tUsersPo.getTelePhone());
        tUsers.setHeaderImg(tUsersPo.getHeaderImg());
        tUsers.setEmail(tUsersPo.getEmail());
        tUsers.setRoleId(Integer.valueOf(tUsersPo.getRoleName()));
        int i = tUsersService.add(tUsers);
        return i;
    }

    @PutMapping(value = "/tUsers/update")
    public Integer tUsersUpdate(@RequestBody TUsersPo tUsersPo) {
        TUsers tUsers = new TUsers();
        tUsers.setId(tUsersPo.getId());
        tUsers.setUserName(tUsersPo.getUserName());
        tUsers.setPassWord(tUsersPo.getPassWord());
        tUsers.setTelePhone(tUsersPo.getTelePhone());
        tUsers.setHeaderImg(tUsersPo.getHeaderImg());
//        tUsers.setCreateTime(tUsersPo.getCreateTime());
        tUsers.setEmail(tUsersPo.getEmail());
        tUsers.setRoleId(Integer.valueOf(tUsersPo.getRoleName()));
        int i = tUsersService.update(tUsers);
        return i;
    }

    @DeleteMapping(value = "/tUsers/{id}")
    public Integer tUsersDelete(@PathVariable(value = "id") int id) {

        int i = tUsersService.delete(id);
        return i;
    }

}
